package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.article.cqrs.ArticlePageQuery;
import com.starlight.flower_server.domain.article.data.ArticleData;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Article;
import com.starlight.flower_server.domain.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/6 8:43
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @GetMapping("/getArticleDetailById")
    public FlowerServerResponse<ArticleData> getArticleDetailById(Long id) {
        return FlowerServerResponse.success(articleService.queryArticleDetailById(id));
    }

    /**
     * 前台展示文章详情
     * @param id
     * @return
     */
    @GetMapping("/showArticleDetailById")
    public FlowerServerResponse<ArticleData> showArticleDetailById(Long id) {
        ArticleData articleData=articleService.queryArticleDetailById(id);
        if(articleData!=null){
            articleService.addArticleHit(id);
        }
        return FlowerServerResponse.success(articleData);
    }

    @PostMapping("/deleteArticleById")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> deleteArticleById(Long id){
        return FlowerServerResponse.success(articleService.logicDeleteArticleById(id));
    }

    @PostMapping("/createArticle")
    public FlowerServerResponse<Integer> createArticle(@RequestBody Article article){
        return FlowerServerResponse.success(articleService.insertArticle(article));
    }

    @PostMapping("/updateArticle")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> updateArticle(@RequestBody Article article){
        return FlowerServerResponse.success(articleService.modifyArticle(article));
    }

    @GetMapping("/getArticlePage")
    public FlowerServerResponse<IPage<ArticleData>> getArticlePage(ArticlePageQuery query){
        return FlowerServerResponse.success(articleService.queryArticleDataList(query));
    }

    @GetMapping("/getAllArticleData")
    public FlowerServerResponse<TotalData<ArticleData>> getAllArticleData(){
        return FlowerServerResponse.success(articleService.queryAllArticleData());
    }

    @GetMapping("/getArticleTotalCount")
    public FlowerServerResponse<TotalData<ArticleData>> getArticleTotalCount(){
        return FlowerServerResponse.success(articleService.queryArticleTotalCount());
    }

    /**
     * 文章随机推送
     * @param count
     * @return
     */
    @GetMapping("/getRandomArticleList")
    public FlowerServerResponse<List<ArticleData>> getRandomArticleList(Integer count){
        if (count==null){
            throw new BadRequestException(Status.PARAMETER_ERROR.getMessage(), Status.PARAMETER_ERROR);
        }
        return FlowerServerResponse.success(articleService.queryRandomArticleList(count));
    }



}
